package com.uenpay.oem.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.oem.R;
import com.uenpay.oem.adapter.ChoosePayTypeAdapter;
import com.uenpay.oem.adapter.RecharageAdapter;
import com.uenpay.oem.data.bean.WrapPayType;
import com.uenpay.service.bean.TransBankCardList;

import java.util.List;

/**
 * Created by PS on 2017/6/18.
 */

public class PayTypeListDialog extends Dialog {


    private List<WrapPayType> mPayTypes;
    private onCloseClickListener closeClickListener; // 点击关闭的回调
    private onAddClickListenner addClickListener; // 点击添加银行卡的回调
    private ImageView dialogClose;
    private TextView dialogAddcard;
    private RecyclerView recyclview;
    private Context mContext;
    private ChoosePayTypeAdapter mAdapter;

    public PayTypeListDialog(Context context, List<WrapPayType> pay) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
        this.mPayTypes = pay;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialogview);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化view
        initview();
        // 数据的填充
        initdata();
        // 初始化界面控件的事件
        initEvent();
    }

    private void initview() {
        dialogClose = (ImageView) findViewById(R.id.dialog_close);
        dialogAddcard = (TextView) findViewById(R.id.dialog_addcard);
        recyclview = (RecyclerView) findViewById(R.id.dialog_recycleview);
    }

    private void initdata() {
        if (mPayTypes != null && mPayTypes.size() > 0) {
            recyclview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
            mAdapter = new ChoosePayTypeAdapter(mContext, mPayTypes);
            //实现监听
            mAdapter.setOnBankItemClickListener(new RecharageAdapter.OnBankItemClickListener() {
                @Override
                public void chooseBank(int position) {
                    if (chooseBankListener != null) {
                        chooseBankListener.choose(position);
                        mAdapter.notifyDataSetChanged();
                    }
                }
            });

            recyclview.setAdapter(mAdapter);

        }
    }

    private void initEvent() {
        // 点击关闭的回调
        dialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (closeClickListener != null) {

                    closeClickListener.oncloseClick();
                }
            }
        });

        // 点击添加银行卡的页面
        dialogAddcard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addClickListener != null) {
                    addClickListener.onaddclick();
                }
            }
        });
    }

    public ChoosePayTypeAdapter getChoosePayTypeAdapter() {
        return mAdapter;
    }

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param
     */
    public void setOnCloseListener(onCloseClickListener onclseclickListener) {

        this.closeClickListener = onclseclickListener;
    }

    /**
     * 设置确定关闭 和添加被点击的接口
     */
    public interface onCloseClickListener {
        void oncloseClick();
    }

    /**
     * 设置添加新卡的跳转的页面
     */
    public void setOnAddListener(onAddClickListenner onAddClickListenner) {
        this.addClickListener = onAddClickListenner;
    }

    public interface onAddClickListenner {
        void onaddclick();
    }

    public interface OnChooseBankListener {
        void choose(int position);
    }

    private OnChooseBankListener chooseBankListener;

    public void setOnChooseBankListener(OnChooseBankListener listener) {
        this.chooseBankListener = listener;
    }
}
